package com.ffms.servlet;

import com.ffms.dao.BankDao;
import com.ffms.dao.impl.BankDaoImpl;
import com.ffms.entity.Bank;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/UpdateBankServlet")
public class UpdateBankServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        //1.接收视图层传来的表单数据并封装成为对象
        String id = request.getParameter("bid");
        String bname = request.getParameter("bname");
        String money = request.getParameter("bmoney");
        Bank bank = new Bank(Integer.parseInt(id), bname, money);

        BankDao bankDao = new BankDaoImpl();
        int n = bankDao.updateBank(bank);
        //3 根据结果跳转到不同页面
        if(n>0){
            //成功则挑转到主页面
            request.getRequestDispatcher("/AllBankServlet").forward(request, response);
        }else{
            //失败则提示
            request.setAttribute("error", "更新银行账户信息失败");
            request.getRequestDispatcher("/PreUpdateBankServlet?id="+id).forward(request, response);
        }
    }
}
